package com.cq.smartelebicycle;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;

import com.cq.smartelebicycle.view.SpeedView;

public class SpeedActivity extends Activity {

	private FrameLayout mContent;
	private SpeedView mSpeedView;
	private final int MAX_PADDING = 0;
	private final int DESC_PADDING = 30;
	private final int MIN_PADDING = 120;
	private int mPadding = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.speed_activity);
		ViewStub stub = (ViewStub) findViewById(R.id.viewStub);
		stub.inflate();
		mContent = (FrameLayout) findViewById(R.id.mContent);
		mSpeedView = (SpeedView) findViewById(R.id.speedView);
		mSpeedView.setScaleView(0);
		mHandler.sendEmptyMessageDelayed(1, 2000);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_plus:
			mPadding -= DESC_PADDING;
			if (mPadding < MAX_PADDING) {
				mPadding = Math.max(mPadding, MAX_PADDING);
				return;
			}
			mContent.setPadding(mPadding, mPadding, mPadding, mPadding);
			mSpeedView.setScaleView(0);
			break;
		case R.id.iv_minus:
			mPadding += DESC_PADDING;
			if (mPadding > MIN_PADDING) {
				mPadding = Math.min(mPadding, MIN_PADDING);
				return;
			}
			mContent.setPadding(mPadding, mPadding, mPadding, mPadding);
			mSpeedView.setScaleView(0);
			break;

		default:
			break;
		}

	}

	private float speed;

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			speed += new Random().nextFloat();
			if (speed >= 35) {
				speed = 0;
			}
			mSpeedView.setSpeed(speed);
			mHandler.sendEmptyMessageDelayed(1, 2000);
		}
	};

}
